package com.smart.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smart.dao.MenuAuthorityDao;
import com.smart.dao.MenuDao;
import com.smart.dao.UserDao;
import com.smart.dao.UserRoleDao;
import com.smart.domain.Login;
import com.smart.domain.Menu;
import com.smart.domain.MenuAuthority;
import com.smart.domain.UserRole;
import com.smart.dto.MenuTreeDto;
import com.smart.service.LoginService;
import com.smart.service.MenuService;
import com.smart.tools.ObjectHelper;

@Service
public class MenuServiceImpl implements  MenuService{
	
	
	@Autowired
	MenuDao menuDao ;

	@Autowired
	UserDao userDao;
	
	@Autowired
	UserRoleDao userRoleDao;
	
	@Autowired
	MenuAuthorityDao menuAuthorityDao;
	
	@Autowired
	LoginService loginService;
	
	@Override
	public List<MenuTreeDto> findMenuTree(String userId) throws Exception {
		List<MenuTreeDto> menuTreeDtos = new ArrayList<MenuTreeDto>();
		//查询用户角色
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("userId", userId);
		List<UserRole> userRoles = userRoleDao.queryUserRole(conditions);
		List<String> menuIds = new ArrayList<>();
		for(UserRole ur : userRoles){
			//查询角色菜单权限
			conditions = new HashMap<String, Object>();
			conditions.put("roleId", ur.getRoleId());
			List<MenuAuthority> authorities = menuAuthorityDao.queryMenuAuthority(conditions);
			for(MenuAuthority ma : authorities){
				menuIds.add(ma.getId());
			}
		}
		
		//查询所有的一级菜单
		conditions = new HashMap<String, Object>();
		conditions.put("level", "1");
		List<Menu> menus = menuDao.queryMenu(conditions);
		for(Menu m: menus){
			if(!menuIds.contains(m.getId())){
				continue;
			}
			MenuTreeDto menuTreeDto = new MenuTreeDto();
			BeanUtils.copyProperties(m, menuTreeDto , new String[]{"childMenu"});
			//查询下级菜单
			conditions = new HashMap<String, Object>();
			conditions.put("parentId", m.getId());
			List<Menu> childMenus = menuDao.queryMenu(conditions);
			List<MenuTreeDto> childMenuDtos = new ArrayList<MenuTreeDto>();
			for(Menu childMenu : childMenus){
				if(!menuIds.contains(childMenu.getId())){
					continue;
				}
				MenuTreeDto childMenuDto = new MenuTreeDto();
				BeanUtils.copyProperties(childMenu, childMenuDto , new String[]{"childMenu"});
				childMenuDtos.add(childMenuDto);
			}
			
			menuTreeDto.setChildMenu(childMenuDtos);
			menuTreeDtos.add(menuTreeDto);
		}
		
		return menuTreeDtos;
	}

	@Override
	public Map<String, Object> mainPage(String userToken) throws Exception {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		
		Map<String, Object> conditions = new HashMap<String, Object>();
		Login login = loginService.findByCond(conditions);
		if(ObjectHelper.isNotEmpty(login)){
			//查询菜单
			List<MenuTreeDto> dtos = findMenuTree(login.getUserId());
			returnMap.put("menuTrees", dtos);
		}
		return returnMap;
	}

	


}
